<?php
class Keywords extends UaModel {
	var $name = "Keyword";
	
	var $type_condition;
	
	function Keywords() {
	
	}
	
	function checkKeyExists($k) {
		$keyword = uaAddSlashes ( $k );
		$this->primaryKey = "title";
		$res = $this->read ( "Keyword.id,Keyword.type AS KeywordType", $keyword );
		if (empty ( $res ) || ! $res) {
			return false;
		} else {
			return $res;
		}
	}
	
	function importKeys($keys, $primary_id = null, $type_id = null, $need_check = false) {
		global $time_stamp;
		global $g_db;
		$split_keys = explode ( ",", $keys );
		
		foreach ( $split_keys as $keyword ) {
			if (! $this->checkKeyExists ( $keyword )) {
				$vals ['title'] = $keyword;
				$vals ['type'] = $type_id;
				$vals ['created'] = $time_stamp;
				$vals ['primary_id'] = $primary_id;
				$vals ['member_id'] = $_SESSION ['MemberID'];
				$vals ['numbers'] = 1;
				$this->save ( $vals );
			} else {
				
				$g_db->Execute ( "update " . $this->getTable () . " set numbers=numbers+1 where title='$keyword'" );
			}
		}
	}
	
	function setCondition($li_id) {
		$s = $this->name . ".status='1'";
		if (empty ( $li_id ) || ! $li_id) {
			$this->type_condition = $s;
		} else {
			$this->type_condition = $s . " and " . $this->name . ".type='" . $li_id . "'";
		}
	}
	
	function getCondition() {
		return $this->type_condition;
	}
}